﻿using MediatR;
using ProduceBasicData.Api.Application.Command.Production;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.Domain.RBAC;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Production
{
    /// <summary>
    /// 产线管理信息查询处理类
    /// </summary>
    public class GetProductionHandler : IRequestHandler<GetProductionCommand, ApiPaging<ProductionDto>>
    {
        private readonly IBaseRespository<ProductionModel> produRepo;
        private readonly IBaseRespository<Workshopmodel> shopRepo;
        private readonly IBaseRespository<UserModel> userRepo;

        public GetProductionHandler(IBaseRespository<ProductionModel> produRepo,
            IBaseRespository<Workshopmodel> shopRepo,
            IBaseRespository<UserModel> userRepo)
        {
            this.produRepo = produRepo;
            this.shopRepo = shopRepo;
            this.userRepo = userRepo;
        }

        public Task<ApiPaging<ProductionDto>> Handle(GetProductionCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ProductionDto> res=new ApiPaging<ProductionDto>();
            var produ = produRepo.GetAll().Where(x=>!x.Isdel);
            var shops = shopRepo.GetAll().Where(x => !x.Isdel);
            var users = userRepo.GetAll().Where(x => !x.Isdel);

            //编码
            if (!string.IsNullOrEmpty(request.Productioncode))
            {
                produ = produ.Where(x=>x.Productioncode.Contains(request.Productioncode));
            }

            //编码
            if (!string.IsNullOrEmpty(request.Productionname))
            {
                produ = produ.Where(x => x.Productionname.Contains(request.Productionname));
            }

            //所属车间
            if (request.Workshopid!=null)
            {
                produ = produ.Where(x => x.Workshopid==request.Workshopid);
            }

            //描述
            if (!string.IsNullOrEmpty(request.Productiondesc))
            {
                produ = produ.Where(x => x.Productiondesc.Contains(request.Productiondesc));
            }

            //创建人
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                produ = produ.Where(x => x.CreateName.Contains(request.CreateName));
            }

            //创建时间
            if (request.Createkstime != null && request.Createjstime != null)
            {
                shops = shops.Where(x => x.CreateTime.AddDays(1) >= request.Createkstime && x.CreateTime <= request.Createjstime);
            }

            //更新时间
            if (request.UpdateksTime != null && request.UpdatejsTime != null)
            {
                shops = shops.Where(x => x.UpdateTime >= request.UpdateksTime && x.UpdateTime <= request.UpdatejsTime);
            }

            var linq = from a in produ
                       join b in shops on a.Workshopid equals b.Id
                       join c in users on b.UserId equals c.Id
                       select new ProductionDto
                       {
                           Id= a.Id,
                           Productioncode = a.Productioncode,
                           Productiondesc = a.Productiondesc,
                           Productionname = a.Productionname,
                           Workshopid = a.Workshopid,
                           Workshopname=b.Workshopname,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           UpdateTime=a.UpdateTime,
                           UserId=b.UserId,
                           Name=c.Name
                       };
            res.PageDatas=linq.OrderByDescending(x=>x.CreateTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.TotalCount=linq.Count();
            res.Code = ApiEnum.Success;
            res.Msg = "查询成功";
            return Task.FromResult(res);
            
        }
    }
}
